<?php

/**
 *  2Moons
 *  Copyright (C) 2011  Slaver
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package 2Moons
 * @author Slaver <slaver7@gmail.com>
 * @copyright 2009 Lucky <lucky@xgproyect.net> (XGProyecto)
 * @copyright 2011 Slaver <slaver7@gmail.com> (Fork/2Moons)
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3 License
 * @version 1.6.1 (2011-11-19)
 * @info $Id: ShowMenuPage.php 2126 2012-03-11 21:11:32Z slaver7 $
 * @link http://code.google.com/p/2moons/
 */
if (!allowedTo(str_replace(array(dirname(__FILE__), '\\', '/', '.php'), '', __FILE__))) exit;
function ShowValutaPage()
{
	global $CONF, $LNG, $USER, $LANG;
	
	$template	= new template();

	$Message	= array();
	$mode 		= HTTP::_GP('mode', '');
	$summa_v	= HTTP::_GP('summa', 0);
	$USER_ID	= HTTP::_GP('id_user', 0);
	$OPLATA_ID	= HTTP::_GP('id', 0);
	$ress	 	= HTTP::_GP('ress', '');
	$action		= HTTP::_GP('action', '');
	$res1		= round(HTTP::_GP('ress1', 0.0), 0);
	$val		= HTTP::_GP('val', 0);
	$time		= HTTP::_GP('time', '', UTF8_SUPPORT);
	

	$not_oplata  = $GLOBALS['DATABASE']->query("SELECT *  FROM ".DONATION." WHERE `status` = 0 ORDER BY `id`;");
	while($row = $GLOBALS['DATABASE']->fetch_array($not_oplata))
	{	
		$summa = $row['sum'] * $row['kurs'];
		if($summa >= 10000 && $summa< 20000) $summa *=1.05;
		elseif($summa >=  20000 && $summa < 50000) $summa *=1.10;
		elseif($summa >=  50000 && $summa < 100000) $summa *=1.20;
		elseif($summa >= 100000 && $summa < 150000) $summa *=1.30;
		elseif($summa >= 150000 && $summa < 200000) $summa *=1.40;
		elseif($summa >= 200000) $summa *=1.5;
		
		$summa *= (1+($row['x_donation'])/100);
		
		$NotOplataList[]	= array(
			'id'			=> $row['id'],
			'id_user'		=> $row['id_user'],
			'name_user'		=> $row['name_user'],
			'sum'			=> $row['sum'],
			'valuta'		=> $row['valuta'],
			'time_user'		=> $row['time_user'],			
			'time'			=> _date($LNG['php_tdformat'], $row['time'], $USER['timezone']),
			'k_oplate'		=> $summa,
			'oplata'		=> "<a href='?page=valuta&amp;mode=donat&amp;id_user=".$row['id_user']."&amp;id=".$row['id']."&amp;summa=".$summa."'>Оплатить</a>",
			'del'			=> "<a href='?page=valuta&amp;mode=del&amp;id=".$row['id']."'>del</a>",
		);
	}	
	$es_oplata  = $GLOBALS['DATABASE']->query("SELECT *  FROM ".DONATION." WHERE `status` = 1 ORDER BY `id` DESC;");
	
	$wmr_s = 0;
	$wmz_s = 0;
	$wme_s = 0;
	$wmu_s = 0;
	$wmb_s = 0;
	$esp_s = 0;
	$yam_s = 0;
	
	while($row = $GLOBALS['DATABASE']->fetch_array($es_oplata))
	{	
		$summa = $row['sum'] * $row['kurs'];
		if($summa >= 10000 && $summa< 20000) $summa *=1.05;
		elseif($summa >=  20000 && $summa < 50000) $summa *=1.10;
		elseif($summa >=  50000 && $summa < 100000) $summa *=1.20;
		elseif($summa >= 100000 && $summa < 150000) $summa *=1.30;
		elseif($summa >= 150000 && $summa < 200000) $summa *=1.40;
		elseif($summa >= 200000) $summa *=1.5;
		
		$summa *= (1+($row['x_donation'])/100);
		
		switch($row['valuta'])
		{
			case 'WMR': 		$wmr_s += $row['sum']; break;
			case 'WMZ': 		$wmz_s += $row['sum']; break;
			case 'WME': 		$wme_s += $row['sum']; break;
			case 'WMU': 		$wmu_s += $row['sum']; break;
			case 'WMB': 		$wmb_s += $row['sum']; break;
			case 'EasyPay': 	$esp_s += $row['sum']; break;
			case 'YandexMoney': $yam_s += $row['sum']; break;
		}
		
		$EsOplataList[]	= array(
			'id'			=> $row['id'],
			'id_user'		=> $row['id_user'],
			'name_user'		=> $row['name_user'],
			'sum'			=> $row['sum'],
			'valuta'		=> $row['valuta'],
			'time_user'		=> $row['time_user'],			
			'time'			=>  _date($LNG['php_tdformat'],$row['time'],"Europe/Minsk"),
			'k_oplate'		=> $summa,
			'oplata'		=> "<a href='?page=valuta&amp;mode=donat_des&amp;id_user=".$row['id_user']."&amp;id=".$row['id']."&amp;summa=".$summa."'>Забрать</a>",
		);
	}	
		
		switch($mode)
		{
			case "donat":
				$SQLQuery	=  "UPDATE ".USERS." SET
								`atm` = `atm` +  ".$summa_v."
								WHERE `id` = '".$USER_ID."';";
				$GLOBALS['DATABASE']->query($SQLQuery);
				$SQLQuery	=  "UPDATE ".DONATION." SET
								`status` = 1
								WHERE `id` = '".$OPLATA_ID."';";
				$GLOBALS['DATABASE']->query($SQLQuery);
				$template->message("Оплачиваю...", 'admin.php?page=valuta', 1);		
			break;
			
			case "donat_des":
				$SQLQuery	=  "UPDATE ".USERS." SET
								`atm` = `atm` -  ".$summa_v."
								WHERE `id` = '".$USER_ID."';";
				$GLOBALS['DATABASE']->query($SQLQuery);
				$SQLQuery	=  "UPDATE ".DONATION." SET
								`status` = 0
								WHERE `id` = '".$OPLATA_ID."';";
				$GLOBALS['DATABASE']->query($SQLQuery);
				$template->message("Забираю...", 'admin.php?page=valuta', 1);	
			break;
			
			case "del":
				$SQLQuery	=  "DELETE FROM ".DONATION." WHERE ".DONATION.".`id` = '".$OPLATA_ID."';";
				$GLOBALS['DATABASE']->query($SQLQuery);
				$template->message("Удаляю...", 'admin.php?page=valuta', 1);	
			break;
			
			case "don":
				$atm 		= HTTP::_GP('atm',0); 
				$id			= HTTP::_GP('id',0);  
						$SQLQuery	=  "UPDATE ".USERS." SET
								`atm` = `atm` +  '".$atm."'
								WHERE `id` = ".$id.";";
						$GLOBALS['DATABASE']->query($SQLQuery);
						$template->message("Добавляю...", 'admin.php?page=valuta', 1);		
			break;
						
			case "all":
				$atm 		= HTTP::_GP('atm',0); 
					$SQLQuery	=  "UPDATE ".USERS." SET
								`atm` = `atm` +  '".$atm."';";
					$GLOBALS['DATABASE']->query($SQLQuery);
					$template->message("Добавляю...", 'admin.php?page=valuta', 1);		
			break;
			case "prem":
				$prem 		= HTTP::_GP('prem_time',0);
				$id			= HTTP::_GP('id',0);  
			
			$user_time  = $GLOBALS['DATABASE']->uniquequery("SELECT `prem_time` FROM ".USERS." WHERE id = '".$id."';");
				
			$prem_time = ($prem * 86400) + max(0, $user_time['prem_time'] - TIMESTAMP) + TIMESTAMP;

			$SQLQuery	=  "UPDATE ".USERS." SET
							`prem_time` = ".$prem_time."
							WHERE `id`  = ".$id.";";
					$GLOBALS['DATABASE']->query($SQLQuery);
					$template->message("Добавляю...", 'admin.php?page=valuta', 1);		
			break;
		}
	
	if ($ress != '')
	{
		switch ($action) 
		{
			case "val":
			
				$wmr		= HTTP::_GP('wmr', 0);
				$wmz		= HTTP::_GP('wmz', 0);
				$wmu		= HTTP::_GP('wmu', 0);
				$wme		= HTTP::_GP('wme', 0);
				$wmb		= HTTP::_GP('wmb', 0);
				$ym			= HTTP::_GP('ym', 0);
				$ep			= HTTP::_GP('ep', 0);
				$x_donation	= HTTP::_GP('x_donation', 0);
				$x_bonus	= HTTP::_GP('x_bonus', 0);
										
				$SQLQuery	=  	"UPDATE ".CONFIG." SET
								`WMR` = ".$wmr.",
								`WMZ` = ".$wmz.",
								`WME` = ".$wme.",
								`WMU` = ".$wmu.",
								`WMB` = ".$wmb.",
								`YandexMoney` = ".$ym.",
								`x_donation` = ".$x_donation.",
								`x_bonus` = ".$x_bonus.",
								`EasyPay` = ".$ep.";";
				$GLOBALS['DATABASE']->query($SQLQuery);
				$template->message("Изменяю курс...", 'admin.php?page=valuta', 1);	
			break;
			default:
			break;
		}	
	}else{
		$template	= new template();
		$template->assign_vars(array(	
			'wmr'				=> $CONF['WMR'],
			'wmz'				=> $CONF['WMZ'],
			'wmu'				=> $CONF['WMU'],
			'wme'				=> $CONF['WME'],
			'wmb'				=> $CONF['WMB'],
			'ep'				=> $CONF['EasyPay'],
			'ym'				=> $CONF['YandexMoney'],
			'wmr_s'				=> $wmr_s,
			'wmz_s'				=> $wmz_s,
			'wmu_s'				=> $wmu_s,
			'wme_s'				=> $wme_s,
			'wmb_s'				=> $wmb_s,
			'esp_s'				=> $esp_s,
			'yam_s'				=> $yam_s,
			'x_donation'		=> $CONF['x_donation'],
			'x_bonus'			=> $CONF['x_bonus'],
			'NotOplataList'   	=> $NotOplataList,
			'EsOplataList'   	=> $EsOplataList,
		));
		$template->show('ShowValutaPage.tpl');
	}
}

?>